﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using NP.BPMReportPlatform.Entities.BPM_Business.CommonReportConfigs;
using NP.BPMReportPlatform.HR.Dto;
using NP.BPMReportPlatform.Reports.CommonReportConfigs.Dtos;
using NP.BPMReportPlatform.Reports.CXYXTHNRA.Dtos;
using NP.BPMReportPlatform.YCLWLJGZSYC.Dto;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;

namespace NP.BPMReportPlatform.Reports.CommonReportConfigs
{
    //[Authorize]
    [ApiController]
    [Route("api/CommonReport")]
    public class CommonReportController : BPMReportPlatformController
    {
        private readonly ICommonReportService _service;
        public CommonReportController(ICommonReportService service)
        {
            _service = service;
        }

        /// <summary>
        /// 查询数据
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost("GetAllAsync")]
        public async Task<PagedResultDto<object>> GetAllAsync([FromBody] CommonReportInput input)
        {
            return await _service.GetAllAsync(input);
        }

        [HttpPost("AddDataAsync")]
        public async Task<IActionResult> AddData([FromBody] CommonReportInput input)
        {
            await _service.AddData(input);
            return Ok();
        }

        [HttpPost("UpdateDataAsync")]
        public async Task<IActionResult> UpdateData([FromBody] CommonReportInput input)
        {
            await _service.UpdateData(input);
            return Ok();
        }

        [HttpPost("DeleteAsync")]
        public async Task<IActionResult> Delete([FromBody] CommonReportInput input)
        {
            await _service.Delete(input);
            return Ok();
        }

        [HttpPost("ExportData")]
        public async Task<FileContentResult> ExportData([FromBody] CommonReportInput input)
        {
            var data = await _service.ExportData(input);
            var ms = new MemoryStream();
            data.Write(ms);
            data.Close();
            return File(ms.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                input.IsTemplate.HasValue && input.IsTemplate.Value ? "导入模板.xlsx" : "导出数据.xlsx");
        }

        [HttpPost("ImportData")]
        public async Task<List<string>> ImportData(IFormFile file, [FromQuery] string moduleId)
        {
            if (file == null)
            {
                return new List<string> { "请上传文件！" };
            }
            var s = file.OpenReadStream();
            var bytes = StreamToBytes(s);
            return await _service.ImportData(bytes, moduleId);
        }
        private byte[] StreamToBytes(Stream stream)
        {
            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);
            // 设置当前流的位置为流的开始
            stream.Seek(0, SeekOrigin.Begin);
            return bytes;
        }

        [HttpPost("GetPageConfigs")]
        public async Task<object> GetPageConfigs([FromQuery] string pageId)
        {
            return await _service.GetPageConfigs(pageId);
        }
    }
}
